//
//  BBphoneInputView.m
//  UILbrary
//
//  Created by Apple on 2023/3/27.
//  Copyright © 2023 mac. All rights reserved.
//

#import "BBphoneInputView.h"
#import "PopSheetView.h"

@interface CountryCodeView : UIView

@property (nonatomic, strong) UILabel *dialingCodeLabel;
@property (nonatomic, strong) UIImageView *iconImg;
@property (nonatomic, strong) UIView *verticalLine;
@property (nonatomic, strong) UIStackView *stackView;
@property (nonatomic, copy) NSString *countryCode;

@end

@implementation CountryCodeView

#pragma mark - Initializer

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configSubviews];
        [self addTap];
        [self test];
    }
    return self;
}

#pragma mark - test
- (void)test{
    self.dialingCodeLabel.text = @"+86";
    self.iconImg.backgroundColor = [UIColor redColor];
    self.verticalLine.backgroundColor = [UIColor redColor];
    self.backgroundColor = [UIColor groupTableViewBackgroundColor];
}

#pragma mark - private

- (void)addTap
{
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    [self addGestureRecognizer:tapGesture];
}

- (void)configSubviews
{
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(50);
    }];
    
    [self addSubview:self.stackView];
    [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.dialingCodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    }];
    
    [self.iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    [self.verticalLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(20);
    }];
    
}

#pragma mark - User Interaction

- (void)handleTapGesture:(UITapGestureRecognizer *)gesture {
    NSArray *arr = @[
        @"中国大陆 +86",
        @"中国香港 +852",
        @"中国大陆 +853",
        @"中国大陆 +886",
    ];
    
    NSArray *mutableArr = @[
        @{@"+86":@"中国大陆 +86"},
        @{@"+852":@"中国香港 +852"},
        @{@"+853":@"中国大陆 +853"},
        @{@"+886":@"中国大陆 +886"}
    ];


    NSMutableArray *allValues = [NSMutableArray array];
    for (NSDictionary *dict in mutableArr) {
        NSArray *values = [dict allValues];
        [allValues addObjectsFromArray:values];
    }
    
    
    [PopSheetView showAlertSheetViewWithTitle:@"选择手机号归属地"
                                  contentList:allValues
                                  selectBlock:^(NSInteger idx, NSString * _Nonnull name) {
        NSDictionary *dic = mutableArr[idx];
        NSString *countryCode = dic.allKeys.firstObject;
        self.dialingCodeLabel.text = countryCode;
        self.countryCode = countryCode;
    }
                                  canselBlock:^{
        
    }];
}

#pragma mark - Lazy Properties

- (UILabel *)dialingCodeLabel {
    if (!_dialingCodeLabel) {
        _dialingCodeLabel = [[UILabel alloc] init];
        _dialingCodeLabel.textAlignment = NSTextAlignmentCenter;
        _dialingCodeLabel.textColor = [UIColor blackColor];
    }
    return _dialingCodeLabel;
}

- (UIImageView *)iconImg {
    if (!_iconImg) {
        _iconImg = [[UIImageView alloc] init];
        _iconImg.contentMode = UIViewContentModeCenter;
    }
    return _iconImg;
}

- (UIView *)verticalLine {
    if (!_verticalLine) {
        _verticalLine = [[UIView alloc] init];
        _verticalLine.backgroundColor = [UIColor grayColor];
    }
    return _verticalLine;
}

- (UIStackView *)stackView {
    if (!_stackView) {
        _stackView = [[UIStackView alloc] initWithArrangedSubviews:@[self.dialingCodeLabel, self.iconImg, self.verticalLine]];
        _stackView.axis = UILayoutConstraintAxisHorizontal;
        _stackView.alignment = UIStackViewAlignmentCenter;
        _stackView.distribution = UIStackViewDistributionFill;
        _stackView.spacing = 5;
    }
    return _stackView;
}



@end


@interface BBphoneInputView()

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) CountryCodeView *countryCodeView;

@end


@implementation BBphoneInputView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        [self configSubViews];
        [self test];
    }
    return self;
}

- (void)configSubViews
{
    [self addSubview:self.textField];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
}

//MARK: - test

- (void)test
{
//    self.countryCodeView.backgroundColor = [UIColor redColor];
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] initWithFrame:CGRectZero];
        _textField.leftView = self.countryCodeView;
        _textField.leftViewMode = UITextFieldViewModeAlways;
        _textField.backgroundColor = [UIColor whiteColor];
    }
    return _textField;
}

- (CountryCodeView *)countryCodeView
{
    if(!_countryCodeView){
        _countryCodeView = [[CountryCodeView alloc] initWithFrame:CGRectZero];
    }
    return _countryCodeView;
}
@end
